package com.jxb.myshop.web.servlet;

import com.jxb.myshop.common.Constant;
import com.jxb.myshop.common.ResponseResult;
import com.jxb.myshop.services.ProductServices;
import com.jxb.myshop.services.impl.ProductServicesImpl;
import com.jxb.myshop.util.StringUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: COS
 * @time: 2022/9/17 9:16
 * @description:
 */
@WebServlet("/pro")
public class ProductController extends BaseController {
    private ProductServices ps = new ProductServicesImpl();
    /**
     *
     * @author: JXB
     * @return:
     * @time: 2022/9/17 10:41
     * @description:
     * 使用商品主键获取商品详情
     */
    public String getProById(HttpServletRequest request, HttpServletResponse response){
        //1 获取用户提交数据
        int pid = StringUtil.StringToInt(request.getParameter("pid"));
        //2 访问业务层获取 详情数据
        ResponseResult rr = ps.getProById(pid);
        //3 存在request作用域中
        request.setAttribute("rr", rr);
        //4 转发
        return Constant.Re.URL_FORWARD + "/goodsDetail.jsp";
    }


    /**
     *
     * @author: JXB
     * @return:
     * @time: 2022/9/17 10:40
     * @description: 商品分页
     */
    public String getProByTid(HttpServletRequest request, HttpServletResponse response){
        //1 获取用户提交的数据
        int tid = StringUtil.StringToInt(request.getParameter("tid"));
        // 当前页码
        int page = StringUtil.StringToInt(request.getParameter("page"));
        //2 访问业务层 获取数据
        ResponseResult all = ps.getProByPageAndTid(page,tid);
        //3 存放到request作用域
        request.setAttribute("rr", all);
        //4 转发到jsp页面
        return Constant.Re.URL_FORWARD +"/goodsList.jsp";
    }

}
